package com.itheima.service;

import com.itheima.common.R;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper1.DishClientMapper;
import com.itheima.mapper1.DishFlavorClientMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.DishFlavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wuyiqiang
 * @2022/6/18 15:47
 */
@Service
public class DishClientService {
    @Autowired
    private DishClientMapper dishClientMapper;
    @Autowired
    private DishFlavorClientMapper dishFlavorClientMapper;

    public R<List<DishDto>> selectAll(String categoryId , Integer status){
        //根据类别查询菜品
        List<Dish> dishList = dishClientMapper.findByCategoryId(categoryId, status);
        //遍历集合获得每个菜品对应的口味
        List<DishDto> dishDtoList= dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //属性拷贝
            BeanUtils.copyProperties(dish,dishDto);
            //查询该菜品的口味
            List<DishFlavor> flavors = dishFlavorClientMapper.selectByDishId(dishDto.getId());
            //讲口味写入dishDto 中
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }
}
